﻿using System.Collections.Generic;
using System.Linq;

namespace Basmus.Conel
{
    public class ListBinding<TElement> : Binding
    {
        public ListBinding()
            : base(typeof(TElement))
        {
        }

        public override object[] GetElements(object container)
        {
            if (container == null)
                return null;
            var elements = (IList<TElement>)container;
            return elements.Cast<object>().ToArray();
        }

        public override object GetContainer(object[] elements)
        {
            if (elements == null)
                return null;
            var container = elements.Select(x => x == null ? default(TElement) : (TElement) x).ToList();
            return container;
        }
    }
}
